package org.elanfox.intellicommunity.msg.server.mq.config;

import org.elanfox.intellicommunity.msg.constant.MsgConstant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author allan
 * @version 1.0.0
 * @date 2019/12/12
 */
@Configuration
public class MqProviderConfig {
    @Autowired
    private AmqpAdmin rabbitAdmin;

    /**
     * 定位消息队列
     *
     * @return
     */
    @Bean(MsgConstant.QUEUE_LOCATION)
    public Queue locationQueue() {
        Queue queue = new Queue(MsgConstant.QUEUE_LOCATION);
        rabbitAdmin.declareQueue(queue);
        return queue;
    }

    @Bean(MsgConstant.QUEUE_COMMAND_REQ)
    public Queue commandReqQueue() {
        Queue queue = new Queue(MsgConstant.QUEUE_COMMAND_REQ);
        rabbitAdmin.declareQueue(queue);

        return queue;
    }

    @Bean(MsgConstant.QUEUE_COMMAND_RESP)
    public Queue commandRespQueue() {
        Queue queue = new Queue(MsgConstant.QUEUE_COMMAND_RESP);
        rabbitAdmin.declareQueue(queue);

        return queue;
    }

    /**
     * 报警消息队列
     *
     * @return
     */
    @Bean(MsgConstant.QUEUE_ALARM)
    public Queue alarmQueue() {
        Queue queue = new Queue(MsgConstant.QUEUE_ALARM);
        rabbitAdmin.declareQueue(queue);
        return queue;
    }

    @Bean(MsgConstant.EXCHANGE_MESSAGE)
    public TopicExchange msgExchange() {
        TopicExchange topicExchange = new TopicExchange(MsgConstant.EXCHANGE_MESSAGE);
        rabbitAdmin.declareExchange(topicExchange);
        return topicExchange;
    }

    @Bean
    public Binding bindingLocationQueue(@Qualifier(MsgConstant.QUEUE_LOCATION) Queue queue, TopicExchange topicExchange) {
        Binding binding = BindingBuilder.bind(queue).to(topicExchange).with(MsgConstant.TOPIC_MSG_LOCATION);
        rabbitAdmin.declareBinding(binding);
        return binding;
    }

    @Bean
    public Binding bindingAlarmQueue(@Qualifier(MsgConstant.QUEUE_ALARM) Queue queue, TopicExchange topicExchange) {
        Binding binding = BindingBuilder.bind(queue).to(topicExchange).with(MsgConstant.TOPIC_MSG_ALARM);
        rabbitAdmin.declareBinding(binding);
        return binding;
    }

    @Bean
    public Binding bindingCommandReqQueue(@Qualifier(MsgConstant.QUEUE_COMMAND_REQ) Queue queue, TopicExchange topicExchange) {
        Binding binding = BindingBuilder.bind(queue).to(topicExchange).with(MsgConstant.QUEUE_COMMAND_REQ);
        rabbitAdmin.declareBinding(binding);
        return binding;
    }

    @Bean
    public Binding bindingCommandRespQueue(@Qualifier(MsgConstant.QUEUE_COMMAND_RESP) Queue queue, TopicExchange topicExchange) {
        Binding binding = BindingBuilder.bind(queue).to(topicExchange).with(MsgConstant.QUEUE_COMMAND_RESP);
        rabbitAdmin.declareBinding(binding);
        return binding;
    }
}
